package com.example.msg.utils;

import android.os.StrictMode;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/*****
 * 文档 https://open.iot.10086.cn/doc/v5/develop/detail/582
 */
public class HttpUtils {
    public static final String TAG = "HELLO-HttpUtils:";
    private static final String url="http://api.heclouds.com/cmds?device_id=925960048";
    private static final String apikey="rHIl=6DPVBVYCGqJ9eiTRGSkgek=";
    private static OkHttpClient client=new OkHttpClient();

    public static String get(String ul,String token){
        if(token==null||"".equals(token)){
            token=apikey;
        }
        try{
            Request request=new Request.Builder().url(ul).header("api-key",token).build();
            Response response=client.newCall(request).execute();
            String str=new String(response.body().bytes(),"UTF-8");
            return str;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static String get(String ul){
        try{
            Request request=new Request.Builder().url(ul).build();
            Response response=client.newCall(request).execute();
            String str=new String(response.body().bytes(),"UTF-8");
            return str;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static InputStream getStream(String ul){
        try{
            Request request=new Request.Builder().url(ul).build();
            Response response=client.newCall(request).execute();
            InputStream in=response.body().byteStream();
            return in;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public static byte[] getByte(String ul){
        try{
            Request request=new Request.Builder().url(ul).build();
            Response response=client.newCall(request).execute();
            byte[] data=response.body().bytes();
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String post(String ul,String token,String json){
        if(token==null||"".equals(token)){
            token=apikey;
        }
        HttpURLConnection connect=null;
        InputStream input=null;
        ByteArrayOutputStream bout=null;
        OutputStream out=null;
        try {
            URL url=new URL(ul);
            connect=(HttpURLConnection)url.openConnection();
            connect.setRequestMethod("POST");
            connect.setRequestProperty("api-key", token);
            //connect.setRequestProperty("api-key", "rHIl=6DPVBVYCGqJ9eiTRGSkgek=");
            connect.setDoOutput(true);
            if(json!=null&&!"".equals(json)) {
                connect.setRequestProperty("Content-type", "application/json");
                out=connect.getOutputStream();
                out.write(json.getBytes("utf-8"));
                out.flush();
            }
            input=connect.getInputStream();
            int len=0;
            byte[]bb=new byte[1024];
            bout=new ByteArrayOutputStream();
            while((len=input.read(bb))>0) {
                bout.write(bb,0,len);
            }
            bb=bout.toByteArray();
            return new String(bb,"UTF-8");

        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                connect.disconnect();
            }catch(Exception e) {
            }

            try {
                input.close();
            }catch(Exception e) {

            }
            try {
                bout.close();
            } catch (Exception e) {
            }
            try {
                if(out!=null) {
                    out.close();
                }
            } catch (Exception e) {
            }
        }
        return null;
    }
}
